package com.topfinance.cnaps2.web.msg;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import com.topfinance.cnaps2.ebo.msg.Header;
import com.topfinance.cnaps2.service.IIBPSManager;
import com.topfinance.cnaps2.util.MsgFactory;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class IBPSAction extends BaseSupport {

	private IIBPSManager ibpsManager = null;

	public void setIbpsManager(IIBPSManager ibpsManager) {
		this.ibpsManager = ibpsManager;
	}

	public IBPSAction() {
		super();
	}

	public String liststruct() {
		log.debug("IBPSAction-liststruct begin");
		try {
			String msgid = request.getParameter("msgid");
			Class o = MsgFactory.getInstance().getMsgPkg(msgid);
			List props = StringUtil.mapClass(o);
			
			// 获得字段-显示对照
			String tableName = MsgFactory.getInstance().getMsgTable(msgid);
			Hashtable fieldHash = ibpsManager.getColumnDef(tableName.toUpperCase());

			// 获得属性-字段对照
			Hashtable propsHash = StringUtil.mapPropsColumn(MsgFactory.getInstance().getMsgMethods(msgid));
			int size = props.size();
			for (int i = 0; i < size; i++) {
				Header h = (Header) props.get(i);
				//log.debug("[Code]" + h.getName());
				if (propsHash.containsKey(h.getName().toUpperCase())) {
					String field = propsHash.get(h.getName().toUpperCase()).toString().toUpperCase();
					String fname = (String) fieldHash.get(field);
					//log.debug("[Field,Comment]" + field + "," + fname);
					h.setColumn(fname);
				}else
					h.setColumn(h.getName());
				props.set(i, h);
			}
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, props);
			rtnResultMap.put(RECORDS_COUNT, props.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("IBPSAction-liststruct end");
		return SUCCESS;
	}

	public String listdata() {
		log.debug("IBPSAction-liststruct begin");
		try {
			String msgid = request.getParameter("msgid");
			Class msg = MsgFactory.getInstance().getMsgPkg(msgid);
			List l = ibpsManager.getList(msg);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("IBPSAction-liststruct end");
		return SUCCESS;
	}

	// public String listibps101() {
	// log.debug("IBPSAction-list begin");
	// try {
	// List l = ibpsManager.getIbps101List();
	// rtnResultMap = new HashMap();
	// rtnResultMap.put(DATA_LIST_ROOT, l);
	// rtnResultMap.put(RECORDS_COUNT, l.size());
	// } catch (Exception e) {
	// handleException(e);
	// }
	// log.debug("IBPSAction-list end");
	// return SUCCESS;
	// }

	// MsgFactory
	public String listmsgtree() {
		log.debug("IBPSAction-list begin");
		try {
			List l = ibpsManager.getMsgTree();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("IBPSAction-list end");
		return SUCCESS;
	}

	public String listmsg() {
		log.debug("IBPSAction-list begin");
		try {
			List l = ibpsManager.getMsgList();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("IBPSAction-list end");
		return SUCCESS;
	}

}